/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  NoiseConfigSave.java                                      
 *  File Contents Outline :  噪声过滤规则保存                  
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */
/*


 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.thu.log.clean;

import cn.edu.thu.log.web.service.impl.PropertiesReaderServicesImpl;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import storage.dbhelper.NoiseIdentifyRuleHelper;
import storage.dbobject.DbNoiseIdentifyRule;
import cn.edu.thu.log.web.service.PropertiesReaderServices;
//import globalparameter.TempFilePosition;
import java.io.*;

/**
 *
 * @author wan
 */
public class NoiseConfigSave {
  private String filename;

    public void writeNoiseConfig(String filename,String treexml) {
//           System.out.println("In the CleanConfigSave!");
//           System.out.println("Begin to write to noise file!");
           OutputStreamWriter osw=null;
           PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        try {
            String filepath = x1.getProperties("filepath") + filename + ".xml";
            osw= new OutputStreamWriter(new FileOutputStream(filepath));
            String xmlhead = x1.getProperties("xml_head");
            String wholexml=xmlhead+treexml;
            osw.write(wholexml);           
            osw.flush();
            
//            System.out.println("Begin to write to cassandra!");
            NoiseIdentifyRuleHelper noiseH=new NoiseIdentifyRuleHelper();
            Date currentTime = new Date();
            DbNoiseIdentifyRule dbnoise = new DbNoiseIdentifyRule(filename, wholexml.toString(), currentTime.toString());
            noiseH.saveNoiseRule(dbnoise);
            osw.close();
            
//            System.out.println("Begin to delete the local noise file");
//            Boolean result=deleteFile(filepath);
//            System.out.println("delete result"+result);
            
        } catch (FileNotFoundException ex) {
            Logger.getLogger(NoiseConfigSave.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(NoiseConfigSave.class.getName()).log(Level.SEVERE, null, ex);       
        }
    }
    
    private boolean deleteFile(String sPath) {
        File file = new File(sPath);
        // 路径为文件且不为空则进行删除   
        if (file.isFile() && file.exists()) {
            file.delete();
           return true;
        }
        return false;
    }
    /**
     * @return the filename
     */
    public String getFilename() {
        return filename;
    }

    /**
     * @param filename the filename to set
     */
    public void setFilename(String filename) {
        this.filename = filename;
    }
    
}
